package com.smartmail.activity;

import java.util.Date;

import com.smartmail.R;
import com.smartmail.SmartMailPreferences;

import android.app.Activity;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TimePicker;

public class GlobalPreferencesActivity extends BaseActivity implements
		OnClickListener {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.options_account);

		ScrollView scroll = (ScrollView) findViewById(R.id.optionsAccountScrollView);

		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

		LinearLayout insideLayout = (LinearLayout) View.inflate(this,
				R.layout.options_global_ins, null);
		scroll.addView(insideLayout);

		periodUseCheckBox = (CheckBox) findViewById(R.id.optionsGlobalPeriodUseCheckBox);
		hintsCheckBox = (CheckBox) findViewById(R.id.optionsGlobalHintsCheckBox);

		periodFrom = (TimePicker) findViewById(R.id.optionsGlobalPeriodFrom);
		periodTo = (TimePicker) findViewById(R.id.optionsGlobalPeriodTo);
		periodFrom.setIs24HourView(true);
		periodTo.setIs24HourView(true);

		messageMaxSizeDownload = (EditText) findViewById(R.id.optionsGlobalMessageSize);
		downloadPeriod = (EditText) findViewById(R.id.optionsGlobalCheckPeriod);

		periodLayout = (LinearLayout) findViewById(R.id.optionsGlobalPeriodLayout);

		/*
		 * Listener to show/hide options connected to period time
		 */
		periodUseCheckBox
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton arg0,
							boolean arg1) {
						if (!arg1) {
							periodLayout.setVisibility(View.GONE);
						} else {
							periodLayout.setVisibility(View.VISIBLE);
							periodFrom.requestFocus();
						}
					}

				});

		saveButton = (Button) findViewById(R.id.optionsAccountSaveButton);
		cancelButton = (Button) findViewById(R.id.optionsAccountCancelButton);

		saveButton.setOnClickListener(this);
		cancelButton.setOnClickListener(this);

		// settings filling
		fillCurrentSettings();

		if (!periodUseCheckBox.isChecked()) {

			periodLayout.setVisibility(View.GONE);

		} else {

			periodLayout.setVisibility(View.VISIBLE);
			periodFrom.requestFocus();

		}

		// /////////////////////////////////////////////////

	}

	/**
	 * Set global properties controls to current values of global properties
	 */
	private void fillCurrentSettings() {

		boolean isSilencePeriodEnabled = SmartMailPreferences
				.getBoolean(SmartMailPreferences.PREFERENCE_IS_SILENCE_PERIOD_ENABLED);
		periodUseCheckBox.setChecked(isSilencePeriodEnabled);

		if (isSilencePeriodEnabled) {
			
			periodLayout.setVisibility(View.VISIBLE);
			periodFrom.requestFocus();

			int silencePeriodBeginning = SmartMailPreferences
					.getInt(SmartMailPreferences.PREFERENCE_SILENCE_PERIOD_BEGINNING);
			periodFrom.setCurrentHour(silencePeriodBeginning / 60);
			periodFrom.setCurrentMinute(silencePeriodBeginning % 60);

			int silencePeriodEnd = SmartMailPreferences
					.getInt(SmartMailPreferences.PREFERENCE_SILENCE_PERIOD_END);
			periodTo.setCurrentHour(silencePeriodEnd / 60);
			periodTo.setCurrentMinute(silencePeriodEnd % 60);

		}
		
		long maxSizeAutoDownload = SmartMailPreferences
				.getLong(SmartMailPreferences.PREFERENCE_MAX_SIZE_OF_AUTO_DOWNLOADED_MESSAGE);
		messageMaxSizeDownload.setText(String
				.valueOf(maxSizeAutoDownload / 1024));
		
		int downloadPeriodInSeconds = SmartMailPreferences
				.getInt(SmartMailPreferences.PREFERENCE_INCOMING_MAIL_SERVER_QUERYING_FREQUENCY);
		downloadPeriod.setText(String.valueOf(downloadPeriodInSeconds / 60));

		boolean showHints = SmartMailPreferences
				.getBoolean(SmartMailPreferences.PREFERENCE_SHOW_HINTS);
		hintsCheckBox.setChecked(showHints);

	}

	/**
	 * Save settings selected by user into SmartMailPreferences
	 */
	private void saveGlobalSettings() {

		boolean isSilencePeriodEnabled = periodUseCheckBox.isChecked();
		SmartMailPreferences.setBoolean(
				SmartMailPreferences.PREFERENCE_IS_SILENCE_PERIOD_ENABLED,
				isSilencePeriodEnabled);

		if (isSilencePeriodEnabled) {

			int silencePeriodBeginning = periodFrom.getCurrentHour() * 60
					+ periodFrom.getCurrentMinute();
			SmartMailPreferences.setInt(
					SmartMailPreferences.PREFERENCE_SILENCE_PERIOD_BEGINNING,
					silencePeriodBeginning);

			int silencePeriodEnd = periodTo.getCurrentHour() * 60
					+ periodTo.getCurrentMinute();
			SmartMailPreferences.setInt(
					SmartMailPreferences.PREFERENCE_SILENCE_PERIOD_END,
					silencePeriodEnd);

		}

		// size auto download is in bytes
		long maxSizeAutoDownload = Long.valueOf(messageMaxSizeDownload
				.getText().toString()) * 1024;
		SmartMailPreferences
				.setLong(SmartMailPreferences.PREFERENCE_MAX_SIZE_OF_AUTO_DOWNLOADED_MESSAGE,
						maxSizeAutoDownload);
		

		// downloading period is in seconds
		int downloadPeriodInSeconds = Integer.valueOf(downloadPeriod.getText()
				.toString()) * 60;
		SmartMailPreferences
				.setInt(SmartMailPreferences.PREFERENCE_INCOMING_MAIL_SERVER_QUERYING_FREQUENCY,
						downloadPeriodInSeconds);

		boolean showHints = hintsCheckBox.isChecked();
		SmartMailPreferences.setBoolean(SmartMailPreferences.PREFERENCE_SHOW_HINTS,
				showHints);

	}

	@Override
	public void onClick(View arg) {
		int id = arg.getId();
		switch (id) {
		case R.id.optionsAccountSaveButton: {

			saveGlobalSettings();
			finish();
			break;

		}
		case R.id.optionsAccountCancelButton: {

			finish();
			break;

		}
		}
	}

	CheckBox periodUseCheckBox;
	CheckBox hintsCheckBox;

	TimePicker periodFrom;
	TimePicker periodTo;

	EditText messageMaxSizeDownload;
	EditText downloadPeriod;

	/**
	 * Layout which is shown/hiden when periodUseCheckBox is unchecked/checked
	 */
	LinearLayout periodLayout;

	Button saveButton;
	Button cancelButton;
}
